package com.huazhi.wzsnw.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.huazhi.wzsnw.mapper.LawMapper;
import com.huazhi.wzsnw.mapper.UserMapper;
import com.huazhi.wzsnw.model.Law;
import com.huazhi.wzsnw.model.User;
import com.huazhi.wzsnw.service.LawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class LawServiceImpl implements LawService {

    @Autowired
    private LawMapper lawMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public int save(Law record) {
        return lawMapper.insertSelective(record);
    }

    @Override
    public int delete(Law record) {
        return lawMapper.deleteByPrimaryKey(record.getTid());
    }

    @Override
    public int deletes(List<Law> records) {
        for (Law law:records){
            delete(law);
        }
        return 0;
    }

    @Override
    public int update(Law record) {
        return lawMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Law findByTid(String tid) {
        return lawMapper.selectByPrimaryKey(tid);
    }

    @Override
    public List<Law> findPagingByListTid(List<String> stringList, Integer pageIndex, Integer pageSize) {
        return lawMapper.findPagingByListTid(stringList, pageIndex, pageSize);
    }

    @Override
    public List<String> getSearchLawList(String key) {
        return lawMapper.getSearchLawList(key);
    }

    @Override
    public List<String> getSearchLawListByRequest(HttpServletRequest request) {
        String selectdata = request.getParameter("selectdata");
        String search = request.getParameter("key");
        List<String> list = new ArrayList<>();
        if(selectdata != null && !"".equals(selectdata)){
            String [] ids = selectdata.split(",");
            list = Arrays.asList(ids);
        }else {
            List<String> params = new ArrayList<String>();
            if (search == null) { search = ""; }
            if ("".equals(search)) {
                params.add("");
            } else if (search.contains(" ")) {
                params = Arrays.asList(search.split(" "));
            } else if (!search.contains(" ")) {
                params.add(search);
            }
            boolean firstSearch = true;
            for (String oneParam : params) {
                if (firstSearch) {
                    list = getSearchLawList(oneParam);
                    firstSearch = false;
                } else {
                    List<String> middleList = getSearchLawList(oneParam);
                    list.retainAll(middleList);
                }
            }
        }
        return list;
    }

    @Override
    public List<JSONObject> getFilesList(String filelist, HttpServletRequest request, String date,String tid) throws IOException {
        List<JSONObject> jsonObjectList = new ArrayList<>();
        String [] files = filelist.split(",");
        for(String path:files){
            JSONObject file = new JSONObject();
            String name = path.substring(path.lastIndexOf("/") + 10,path.lastIndexOf("."));
            file.put("filename",name);
            file.put("filetype",path.substring(path.lastIndexOf(".") + 1));
//            File file1 = new File(request.getServletContext().getRealPath(path.replace(request.getContextPath() + "/","")));
            URL url = new URL(path);
            HttpURLConnection urlcon2 = (HttpURLConnection) url.openConnection();
            double length = Double.parseDouble(urlcon2.getHeaderField("Content-Length"));
//            double length = file1.length();
            if(length > 1024){
                length = length / 1024;
                if(length > 1024){
                    length = length / 1024;
                    if(length > 1024){
                        length = length / 1024;
                        file.put("filesize",(int)Math.ceil(length) + "GB");
                    }else {
                        file.put("filesize",(int)Math.ceil(length) + "MB");
                    }
                }else {
                    file.put("filesize",(int)Math.ceil(length) + "KB");
                }
            }else {
                file.put("filesize",(int)Math.ceil(length) + "B");
            }
            file.put("filepath",path);
            file.put("uploadtime",date);
            file.put("name",path.substring(path.lastIndexOf("/") + 10));
            User user = userMapper.selectByPrimaryKey(tid);
            String username = "";
            if(user != null){
                username = user.getName();
            }
            file.put("uploaduser",username);
            jsonObjectList.add(file);
        }
        return jsonObjectList;
    }
}
